<?php

namespace App\Models;


use EloquentFilter\Filterable;
use Illuminate\Notifications\Notifiable;

class Role extends BaseModel
{
    use Notifiable, Filterable;

    /**
     * @var string
     */
    protected $table = 'role';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'parent_id'
    ];


    /**
     * 所拥有权限
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class, 'role_permission');
    }

    /**
     * 所拥有目录
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function menus()
    {
        return $this->belongsToMany(Menus::class, 'role_menus')->orderBy('weight', 'DESC');
    }

    /**
     * 上级
     * @return mixed
     */
    public function parent()
    {
        return $this->belongsTo(Role::class);
    }

    /**
     * 获取子集
     * @return mixed
     */
    public function children()
    {
        return $this->hasMany(Role::class, 'parent_id', 'id');
    }


    /**
     * 所拥有总后台目录
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function adminMenus()
    {
        return $this->menus()->where('parent_id', '0');
    }


    /**
     * 获取id
     * @return array
     */
    public function getMenusIdAttribute()
    {
        return $this->menus()->pluck('id')->toArray();
    }


    /**
     * 获取名称
     * @return array
     */
    public function getPermissionListAttribute()
    {
        return $this->permissions()->pluck('name')->toArray();
    }



    /**
     * 获取id
     * @return array
     */
    public function getPermissionIdAttribute()
    {
        return $this->permissions()->pluck('id')->toArray();
    }
}
